{% extends "base.html" %}

{% block title %}课程表{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fas fa-calendar-alt"></i> 我的课程表</h2>
        <a href="{{ url_for('dashboard') }}" class="btn btn-outline-primary">
            <i class="fas fa-home"></i> 返回主页
        </a>
    </div>
    
    {% if courses %}
    <div class="table-responsive">
        <table class="table">
            <thead>
                <tr>
                    <th><i class="fas fa-book"></i> 课程名称</th>
                    <th><i class="fas fa-map-marker-alt"></i> 教室</th>
                    <th><i class="fas fa-user-tie"></i> 任课教师</th>
                    <th><i class="fas fa-clock"></i> 上课时间</th>
                    <th><i class="fas fa-cog"></i> 操作</th>
                </tr>
            </thead>
            <tbody>
                {% for course in courses %}
                <tr>
                    <td><strong>{{ course.course_name }}</strong></td>
                    <td><span class="badge bg-info">{{ course.classroom }}</span></td>
                    <td>{{ course.teacher }}</td>
                    <td><span class="badge bg-warning">{{ course.time_slot }}</span></td>
                    <td>
                        <button class="btn btn-danger btn-sm" onclick="deleteCourse('{{ course.course_name }}', '{{ course.time_slot }}')"
                                title="删除课程">
                            <i class="fas fa-trash"></i> 删除
                        </button>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    {% else %}
    <div class="alert alert-info text-center">
        <i class="fas fa-info-circle fa-2x mb-3"></i>
        <h4>暂无课程</h4>
        <p>您还没有添加任何课程，快来添加第一门课程吧！</p>
    </div>
    {% endif %}
    
    <!-- 添加课程表单 -->
    <div class="card mt-4">
        <div class="card-body">
            <h3 class="card-title"><i class="fas fa-plus-circle"></i> 添加新课程</h3>
            <form method="POST" action="/add_course" class="row g-3">
                <div class="col-md-6">
                    <label for="course_name" class="form-label">
                        <i class="fas fa-book"></i> 课程名称
                    </label>
                    <input type="text" class="form-control" id="course_name" name="course_name" 
                           placeholder="请输入课程名称" required>
                </div>
                <div class="col-md-6">
                    <label for="classroom" class="form-label">
                        <i class="fas fa-map-marker-alt"></i> 教室
                    </label>
                    <input type="text" class="form-control" id="classroom" name="classroom" 
                           placeholder="请输入教室位置" required>
                </div>
                <div class="col-md-6">
                    <label for="teacher" class="form-label">
                        <i class="fas fa-user-tie"></i> 任课教师
                    </label>
                    <input type="text" class="form-control" id="teacher" name="teacher" 
                           placeholder="请输入教师姓名" required>
                </div>
                <div class="col-md-6">
                    <label for="time_slot" class="form-label">
                        <i class="fas fa-clock"></i> 上课时间
                    </label>
                    <input type="text" class="form-control" id="time_slot" name="time_slot" 
                           placeholder="例如：周一 1-2节" required>
                </div>
                <div class="col-12 text-center">
                    <button type="submit" class="btn btn-primary btn-lg">
                        <i class="fas fa-plus"></i> 添加课程
                    </button>
                </div>
            </form>
        </div>
    </div>
    
    <div class="text-center mt-4">
        <a href="/dashboard" class="btn btn-secondary btn-lg">
            <i class="fas fa-home"></i> 返回主页
        </a>
    </div>
</div>

<script>
function deleteCourse(courseName, timeSlot) {
    if (confirm('确定要删除课程 "' + courseName + '" 吗？')) {
        fetch('/delete_course', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                course_name: courseName,
                time_slot: timeSlot
            })
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                location.reload();
            } else {
                alert('删除失败：' + data.message);
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('删除失败，请重试');
        });
    }
}
</script>
{% endblock %}